package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.City;
import com.wm.bean.Restaurant;
import com.wm.bean.RestaurantTime;
import com.wm.bean.Scriptorium;
import com.wm.comm.Constant;
import com.wm.service.CityService;
import com.wm.service.RestaurantService;
import com.wm.service.RestaurantTimeService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.ui.OptionsInt;

public class AdminRestaurantTime extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminRestaurantTime.class);
	
	private RestaurantTimeService restaurantTimeService;
	private RestaurantService restaurantService;

	public RestaurantTimeService getRestaurantTimeService() {
		return restaurantTimeService;
	}

	public void setRestaurantTimeService(RestaurantTimeService restaurantTimeService) {
		this.restaurantTimeService = restaurantTimeService;
	}

	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}

	private List isShowList = new ArrayList();
	
	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}

	

	private void setIsShowListValues(){
		isShowList = Constant.IS_SHOW;
	}

	
	private void initValues(){
		setIsShowListValues();
	}
	
	private void listInitValues(){
		setIsShowListValues();
		this.restaurant = this.getRestaurantService().findRestaurantById(this.resId);
	}


	private long id;					//主键ID
	private long resId;					//饭店Id
	private int startHour;				//开始小时
	private int startMinute;			//开始分钟
	private int endHour;				//结束小时
	private int endMinute;				//结束分钟
	private String timeMemo;			//备注
	private int isShow = -1;			//是否显示
	private int orderNum;				//排序字段
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getResId() {
		return resId;
	}

	public void setResId(long resId) {
		this.resId = resId;
	}

	public int getStartHour() {
		return startHour;
	}

	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}

	public int getStartMinute() {
		return startMinute;
	}

	public void setStartMinute(int startMinute) {
		this.startMinute = startMinute;
	}

	public int getEndHour() {
		return endHour;
	}

	public void setEndHour(int endHour) {
		this.endHour = endHour;
	}

	public int getEndMinute() {
		return endMinute;
	}

	public void setEndMinute(int endMinute) {
		this.endMinute = endMinute;
	}

	public String getTimeMemo() {
		return timeMemo;
	}

	public void setTimeMemo(String timeMemo) {
		this.timeMemo = timeMemo;
	}

	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public int getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}
	
	private Restaurant restaurant;
	private List timeList;

	public Restaurant getRestaurant() {
		return restaurant;
	}

	public void setRestaurant(Restaurant restaurant) {
		this.restaurant = restaurant;
	}

	public List getTimeList() {
		return timeList;
	}

	public void setTimeList(List timeList) {
		this.timeList = timeList;
	}


	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		this.setTimeList(this.getRestaurantTimeService().findRestaurantTimesByResIdIsShow(this.resId, this.isShow, "id", 0));
		return SUCCESS;
	}
	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加
		RestaurantTime restaurantTime = new RestaurantTime();
		restaurantTime.setResId(this.resId);
		restaurantTime.setStartHour(this.startHour);
		restaurantTime.setStartMinute(this.startMinute);
		restaurantTime.setEndHour(this.endHour);
		restaurantTime.setEndMinute(this.endMinute);
		restaurantTime.setTimeMemo(this.timeMemo);
		restaurantTime.setIsShow(this.isShow);
		restaurantTime.setOrderNum(this.orderNum);
		restaurantTime.setOrderTime(new Date());
		restaurantTime.setAddTime(new Date());
		try {
			this.getRestaurantTimeService().saveRestaurantTime(restaurantTime);
		} catch (Exception e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		RestaurantTime restaurantTime = this.getRestaurantTimeService().findRestaurantTimeById(this.id);
		this.setResId(restaurantTime.getResId());;
		this.setStartHour(restaurantTime.getStartHour());
		this.setStartMinute(restaurantTime.getStartMinute());
		this.setEndHour(restaurantTime.getEndHour());
		this.setEndMinute(restaurantTime.getEndMinute());
		this.setTimeMemo(restaurantTime.getTimeMemo());
		this.setIsShow(restaurantTime.getIsShow());
		this.setOrderNum(restaurantTime.getOrderNum());
		return INPUT;
	}
	
	public String doupdate(){
		//添加商品
		RestaurantTime restaurantTime = this.getRestaurantTimeService().findRestaurantTimeById(this.id);
		restaurantTime.setResId(this.resId);
		restaurantTime.setStartHour(this.startHour);
		restaurantTime.setStartMinute(this.startMinute);
		restaurantTime.setEndHour(this.endHour);
		restaurantTime.setEndMinute(this.endMinute);
		restaurantTime.setTimeMemo(this.timeMemo);
		restaurantTime.setIsShow(this.isShow);
		restaurantTime.setOrderNum(this.orderNum);
		restaurantTime.setOrderTime(new Date());
		restaurantTime.setAddTime(new Date());
		try {
			this.getRestaurantTimeService().updateRestaurantTime(restaurantTime);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String orders(){
		RestaurantTime restaurantTime = this.getRestaurantTimeService().findRestaurantTimeById(this.id);
		restaurantTime.setOrderTime(new Date());
		try {
			this.getRestaurantTimeService().updateRestaurantTime(restaurantTime);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getRestaurantTimeService().removeRestaurantTimeById(this.id);
		return "index";
	}	
}
